<template>
  <div>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <span>用户注册</span>
      </div>
      <div>
        <el-form ref="form" :model="resgister" label-width="80px">
          <el-form-item label="头像">
            <el-upload
              class="avatar-uploader"
              action="http://localhost:8080/uploadoss"
              :show-file-list="false"
              :on-success="handleAvatarSuccess"
              :before-upload="beforeAvatarUpload"
            >
              <img
                v-if="resgister.userImg"
                :src="resgister.userImg"
                class="avatar"
              />
              <i v-else class="el-icon-plus avatar-uploader-icon"></i>
            </el-upload>
          </el-form-item>

          <el-form-item label="用户名">
            <el-input
              v-model="resgister.userName"
              placeholder="请输入用户名"
            ></el-input>
          </el-form-item>

          <el-form-item label="手机号码">
            <el-input
              v-model="resgister.tel"
              placeholder="请输入注册手机号码"
            ></el-input>
          </el-form-item>
          <el-form-item label="密码">
            <el-input
              v-model="resgister.password"
              placeholder="请输入密码"
              show-password
            ></el-input>
          </el-form-item>
          <el-form-item label="确认密码">
            <el-input
              v-model="password"
              placeholder="请重复输入密码"
              show-password
            ></el-input>
          </el-form-item>

          <el-form-item>
            <el-button type="primary" @click="toRegister" style="width: 140px"
              >注册</el-button
            >
            <el-button @click="toUserAdmin" style="width: 140px"
              >首页</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      password: "",
      resgister: {
        userImg: false,
        tel: "",
        password: "",
        userName: "",
      },
    };
  },
  methods: {
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
    handleAvatarSuccess(res, file) {
      this.resgister.userImg = URL.createObjectURL(file.raw);
    },
    toRegister() {
      if (this.password != this.resgister.password) {
        this.$message.error("两次密码不一致");
        return;
      } else {
        this.$axios.post("user/register", this.resgister).then((res) => {
          if (res.data.code == 200) {
            this.$message.success("注册成功");
            this.$router.push("/");
          } else {
             this.$message.error(res.data.message);
          }
        });
      }
    },
    toUserAdmin() {
      this.$router.push("/UserAdmin");
    },
  },
};
</script>
<style>
<style > .avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}

.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both;
}

.box-card {
  width: 480px;
  margin-left: 530px;
  margin-top: 50px;
}
</style>